/********************************************************************************************************************
** Contact: yashvpatil@gmail.com
**
** GNU General Public License Usage
** This file may be used under the terms of the GNU
** General Public License versions 2.0 or 3.0 as published by the Free Software Foundation.
** Please review the following information to ensure GNU General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
*********************************************************************************************************************/

#include "qrangewidget.h"

#define _RANGE_WIDGET_HEIGHT	27
#define _RANGE_WIDGET_WIDTH		32

#define _TOWERLINE_OFFSET_X		1
#define _TOWERLINE_OFFSET_Y		2

#define _TOWERLINE_SPACING		5
#define _TOWER_PENWIDTH			3

QRangeWidget::QRangeWidget(int x, int y, QWidget *parent):QWidget(parent)
{
	// Default range is norange
	value = NORANGE;
	orientation = HORIZONTAL;

	// Set widget geometry
	setGeometry(parent->x() + x, parent->y() + y, _RANGE_WIDGET_WIDTH, _RANGE_WIDGET_HEIGHT);

	// Set window flags to hide min/max buttons, title and frameless window
	setWindowFlags(windowFlags() ^ Qt::WindowMinMaxButtonsHint);
	setWindowFlags(windowFlags() ^ Qt::WindowTitleHint);
	setWindowFlags(windowFlags() | Qt::FramelessWindowHint);

	// Set default pen color to blue
	pen.setColor(Qt::blue);

	// Set pen width
	pen.setWidth(_TOWER_PENWIDTH);

}

QRangeWidget::~QRangeWidget()
{

}


void QRangeWidget::setPenWidth(int w)
{
	pen.setWidth(w);
}


void QRangeWidget::setLevel(Level l)
{
	// Store new range value
	value = l;

	// Repaint the widget
	update();
}

void QRangeWidget::setOrientation(Orientation o)
{
	orientation = o;
}


void QRangeWidget::setForeColor(QColor color)
{
	// Store pen color
	pen.setColor(color);

	// Repaint the widget
	update();
}

void QRangeWidget::setBackColor(QColor color)
{
	// Set palette color
	QPalette pal = palette();
	pal.setColor(QPalette::Window, color);
	setPalette(pal);

	// Repaint the widget
	update();
}


QRangeWidget::Level QRangeWidget::currentRange(QString &s)
{
	int i = (int)value;
	switch(i)
	{
		case 0: s = "No Range"; break;
		case 1: s = "Poor"; break;
		case 2: s = "Low"; break;
		case 3: s = "Good"; break;
		case 4: s = "Excellent"; break;
		case 5: s = "Outstanding"; break;
	}

	// Return current range value
	return value;
}


void QRangeWidget::paintEvent(QPaintEvent *)
{
	// Create painter
	QPainter painter(this);

	// Erase widget's rectangular client area
	painter.eraseRect(rect());

	// Set pen for painter
	painter.setPen(pen);

	// Draw towers
	drawTowers(painter);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

QRangeWidget_Bar::QRangeWidget_Bar(int x, int y, QWidget *parent):QRangeWidget(x, y, parent)
{
}

QRangeWidget_Bar::~QRangeWidget_Bar()
{
}

void QRangeWidget_Bar::drawTowers(QPainter & painter)
{
	int x1 = 0, y1 = 0, x2 = 0, y2 = 0;

	// Drawing logic
	for (int i=0; i<=(int)value; i++)
	{
		if (i>0)
		{
			switch(orientation)
			{
				case HORIZONTAL:
				{
					x1 = _TOWERLINE_OFFSET_X + (i * width()/5);
					y1 = height() - _TOWERLINE_OFFSET_Y;
					x2 = _TOWERLINE_OFFSET_X + (i * width()/5 );
					y2 = height() + _TOWERLINE_OFFSET_Y - (i*height()/5) ;
				}
				break;
				case VERTICAL:
				{
					x1 = _TOWERLINE_OFFSET_X + 5;
					y1 = _TOWERLINE_OFFSET_Y + (height()/5 * i);
					x2 = _TOWERLINE_OFFSET_X + (i * width()/5) + 5;
					y2 = _TOWERLINE_OFFSET_Y + (height()/5 *i);
				}
				break;
				case HORIZONTAL_R:
				{
					x1 = width() - ((i-1) * width()/5);
					y1 = height() - _TOWERLINE_OFFSET_Y;
					x2 = width() - ((i-1) * width()/5);
					y2 = height() - (_TOWERLINE_OFFSET_Y + ((i-1)*height()/5));
				}
				break;

				case VERTICAL_R:
				{
					x1 = _TOWERLINE_OFFSET_X + 5;
					y1 = height() + (_TOWERLINE_OFFSET_Y + 3) - (height()/5 * i);
					x2 = _TOWERLINE_OFFSET_X + (i * width()/5) + 5;
					y2 = height() + (_TOWERLINE_OFFSET_Y + 3) - (height()/5 * i);

				}
				break;
			}

			// Draw bar tower
			painter.drawLine(x1, y1, x2, y2);
		}
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

QRangeWidget_Arc::QRangeWidget_Arc(int x, int y, QWidget *parent):QRangeWidget(x, y, parent)
{
}

QRangeWidget_Arc::~QRangeWidget_Arc()
{
}

void QRangeWidget_Arc::drawTowers(QPainter & painter)
{
	QRectF rectangle;
	int startAngle = 0;
	int spanAngle = 0;

	qreal x1, y1, x2, y2;


	// Drawing Arc logic
	for (int i=0; i<=(int)value; i++)
	{
		if (i>0)
		{
			switch(orientation)
			{
				case HORIZONTAL:
				{
					x1 = (i-1) * width()/5;
					y1 = height()/2 - (i* 2.0);

					x2 = x1 + width()/5;
					y2 = height()/2 + (i* 2.0);

					startAngle = -90 * 16;
					spanAngle = 180 * 16;
				}
				break;
				case VERTICAL:
				{
					x1 = width()/2 - (i*2.0);
					y1 = height() - (i-1)*height()/5;

					x2 = width()/2 + (i*2.0);
					y2 = y1 - height()/5;

					startAngle = 0 * 16;
					spanAngle = 180 * 16;

				}
				break;
				case HORIZONTAL_R:
				{
					x1 = width() - (i-1) * width()/5;
					y1 = height()/2 - (i* 2.0);

					x2 = x1 - width()/5;
					y2 = height()/2 + (i* 2.0);

					startAngle = 90 * 16;
					spanAngle = 180 * 16;
				}
				break;
				case VERTICAL_R:
				{
					x1 = width()/2 - (i*2.0);
					y1 = (i-1)*height()/5;

					x2 = width()/2 + (i*2.0);
					y2 = y1 + height()/5;

					startAngle = 0 * 16;
					spanAngle = 180 * 16;

				}
				break;
			}
		}

		// Draw Arc
		rectangle.setCoords(x1, y1, x2, y2);
		painter.drawArc(rectangle, startAngle, spanAngle);
		//painter.drawRect(rectangle);
	}
}
